package show.me.the.money.service.dao;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import show.me.the.money.config.reportSchema.ReportSchemaController;
import show.me.the.money.config.reportSchema.define.ReportSchema;
import show.me.the.money.service.core.Service;
import show.me.the.money.util.context.Context;
import show.me.the.money.util.context.HashMapContext;

@SuppressWarnings({"unchecked","unused"})
public class SimpleReport implements Service{

	public void execute(HashMap<String,Object> req,HashMap<String,Object> res,Context ctx) {
		int code = 200;
		String msg = "Success";
		Context qCtx = new HashMapContext(req);
		ctx.putCtx("q", qCtx);
		String id = (String)req.get("schema");
		ReportSchema reportSchema = ReportSchemaController.instance().getSchemaDynamic(id, ctx);
		if(reportSchema != null){
			try{
				List<HashMap> data = reportSchema.run(ctx);
				HashMap<String,Object> returnBody=new HashMap<String,Object>();
				res.put("body", data);
				res.put("schema", reportSchema.json(ctx));
				res.put("totalCount", ctx.value("totalCount"));
				res.put("pageSize", ctx.value("pageSize"));
				res.put("pageNo", ctx.value("pageNo"));
			}
			catch(Exception e){
				e.printStackTrace();
				code = 502;
				msg = "QueryFailed";
			}
		}
		else{
			code = 404;
			msg = "NoSuchSchema";
		}
		res.put(RES_CODE, code);
		res.put(RES_MESSAGE, msg);
	}
	

}
